Fork me on GitHub

SpringMVC配置Swagger构建Api文档

版本描述

Spring4.+, springfox-swagger2 2.6

SpringMVC整合Swagger-ui

添加jar

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.0</version>
</dependency>
<!-- swagger-ui 为项目提供api展示及测试的界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>

配置Swagger

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.bmkit.controller")
public class SwaggerConfig {
@Bean
public Docket getDocket(){

Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(regex("/marker/.*"))
.build()
.pathMapping("/");
return docket;
}

public ApiInfo apiInfo(){
ApiInfo apiInfo=new ApiInfo(
"SpringMVC整合Swagger-ui API",
"SpringMVC整合Swagger-ui API",
"1.1",
"",
contact(),
"",
"");
return apiInfo;
}

public Contact contact(){
Contact contact=new Contact("yuanwj","","yuanwj@gmail.com");
return contact;
}
}


@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}

访问

http://localhost:port/swagger-ui.html

效果

SpringMVV+Swagger-ui效果图

显示 Gitment 评论